import { ACTIVITY_OWNER_VALUE } from '../constants/activity';
import { formatDecimal, getTimeDuration } from '../common/utils';

/**
 * 处理活动数据分析
 * @param ownerInfo 拥有者信息
 */
export const dealActivityStats = (owner, statsInfo = {}) => {
    const { exposure, order } = statsInfo;
    const stats = { all: {}, self: {}, help: {} };
    if (owner === ACTIVITY_OWNER_VALUE.brand) {
        // self
        stats.self = {
            order: {
                orderCount: order.own.orderCount,
                orderAmount: formatDecimal(order.own.orderAmount),
                commissionAmount: formatDecimal(order.own.wxpayCommission + order.own.platformCommission + order.own.refundWxpayCommission + order.own.refundPlatformCommission),
                refundOrderAmount: formatDecimal(order.own.refundOrderAmount),
            },
            exposure: {
                uvCount: exposure.own.uv,
                buyerCount: order.own.buyerCount,
                buyRate: formatDecimal(order.own.buyerCount / (exposure.own.uv || 1) * 100),
            },
        };
        stats.self.order.incomeAmount = formatDecimal(stats.self.order.orderAmount + stats.self.order.commissionAmount + stats.self.order.refundOrderAmount);
        // help
        stats.help = {
            order: {
                orderCount: order.endorsement.orderCount,
                orderAmount: formatDecimal(order.endorsement.orderAmount),
                commissionAmount: formatDecimal(order.endorsement.wxpayCommission + order.endorsement.platformCommission + order.endorsement.refundWxpayCommission + order.endorsement.refundPlatformCommission),
                refundOrderAmount: formatDecimal(order.endorsement.refundOrderAmount),
                distributeAmount: formatDecimal(order.endorsement.distributeAmount + order.endorsement.refundDistributeAmount),
                bonusAmount: formatDecimal(order.endorsement.orderBonus + order.endorsement.refundOrderBonus),
            },
            exposure: {
                uvCount: exposure.endorsement.uv,
                buyerCount: order.endorsement.buyerCount,
                buyRate: formatDecimal(order.endorsement.buyerCount / (exposure.endorsement.uv || 1) * 100),
            },
        };
        stats.help.order.incomeAmount = formatDecimal(stats.help.order.orderAmount + stats.help.order.commissionAmount + stats.help.order.refundOrderAmount + stats.help.order.distributeAmount + stats.help.order.bonusAmount);
        // all
        stats.all = {
            order: {
                orderCount: stats.self.order.orderCount + stats.help.order.orderCount,
                orderAmount: formatDecimal(stats.self.order.orderAmount + stats.help.order.orderAmount),
                commissionAmount: formatDecimal(stats.self.order.commissionAmount + stats.help.order.commissionAmount),
                refundOrderAmount: formatDecimal(stats.self.order.refundOrderAmount + stats.help.order.refundOrderAmount),
                distributeAmount: stats.help.order.distributeAmount,
                bonusAmount: stats.help.order.bonusAmount,
            },
            exposure: {
                uvCount: formatDecimal(stats.self.exposure.uvCount + stats.help.exposure.uvCount),
                buyerCount: formatDecimal(stats.self.exposure.buyerCount + stats.help.exposure.buyerCount),
                buyRate: formatDecimal((stats.self.exposure.buyerCount + stats.help.exposure.buyerCount) / ((stats.self.exposure.uvCount + stats.help.exposure.uvCount) || 1) * 100),
            },
        };
        stats.all.order.incomeAmount = formatDecimal(stats.self.order.incomeAmount + stats.help.order.incomeAmount);
    } else if (owner === ACTIVITY_OWNER_VALUE.spokesman) {
        // help
        stats.self = {
            order: {
                orderCount: order.orderCount,
                orderAmount: formatDecimal(order.orderAmount),
                commissionAmount: formatDecimal(order.wxpayCommission + order.platformCommission + order.refundWxpayCommission + order.refundPlatformCommission),
                refundOrderAmount: formatDecimal(order.refundOrderAmount),
                bonusAmount: formatDecimal(order.orderBonus + order.refundOrderBonus),
            },
            exposure: {
                uvCount: exposure.uv,
                buyerCount: order.buyerCount,
                buyRate: formatDecimal(order.buyerCount / (exposure.uv || 1) * 100),
            },
        };
        stats.self.order.incomeAmount = Math.abs(stats.self.order.bonusAmount);
    }
    return stats;
};

/**
 * 处理帮卖统计列表
 * @param {Array} list 数据列表
 */
export const dealStatsConsignmentListData = (list) => {
    return list.map(item => {
        const { published, orderAmount, orderBonus, orderCount } = item;
        return {
            id: item.spokesmanUserId,
            pic: item.spokesmanUserPic,
            name: item.spokesmanUserNickName,
            mobile: item.spokesmanUserMobile,
            date: getTimeDuration(published, '前'),
            orderAmount,
            orderBonus,
            orderCount,
        };
    });
};

/**
 * 处理代言人排名列表
 * @param {Array} list 数据列表
 */
export const dealSpokesmanRankListData = (list) => {
    return list.map(item => {
        const { orderAmount, orderAmountRank, orderBonus, refundOrderAmount } = item;
        return {
            id: item.spokesmanUserId,
            name: item.spokesmanUserNickName,
            orderAmount,
            orderAmountRank,
            orderBonus,
            refundOrderAmount,
        };
    });
};
